#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define str string
#define all(x) (x).begin(),(x).end()
#define ms(a,t) memset(a,t,sizeof(a))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define db double
const int maxn = 1200+10;
const int mod = 998244353;
const ll INF = 0x3f3f3f3f;
const db eps = 1e-7;
int n,m;
int p[maxn],v[maxn];
ll dep[maxn];
vector<int> G[maxn];
ll cur;
vector<pll > f[maxn];
void dfs(int x) {
	f[x].clear();
	if(G[x].size() == 0) {
		f[x].pb(MP(dep[x],dep[x]));
		return;
	}
	dfs(G[x][0]); dfs(G[x][1]);
	vector<pll > v1,v2;
	int l = G[x][0],r = G[x][1];
	for(int i = 0,j = f[r].size()-1,pre = INF; i < (int)f[l].size(); i++) {
		while(j >= 0 && f[l][i].fi + f[r][j].fi-2*dep[x] > cur) j--;
		if(j >= 0 && j != pre) {
			v1.pb(MP(f[l][i].se,f[r][j].se));
		}
//		pre = j;
	}
	for(int i = 0,j = f[l].size()-1,pre = INF; i < (int)f[r].size(); i++) {
		while(j >= 0 && f[r][i].fi + f[l][j].fi-2*dep[x] > cur) j--;
//		cerr<<i<<' '<<j<<' '<<f[r][i].fi<<'\n';
		if(j >= 0 && j != pre) {
			v2.pb(MP(f[r][i].se,f[l][j].se));
		}
//		pre = j;
	}
//	cerr<<x<<' '<<f[x].size()<<' '<<cur<<'\n';
	f[x].resize(v1.size()+v2.size());
	reverse(all(v1)); reverse(all(v2));
	merge(all(v1),all(v2),f[x].begin());
}
bool check(ll x) {
	cur = x;
	dfs(1);
	return (!f[1].empty());
}
void Solve() {
	cin >> n; ll sum = 0;
	for(int i = 2; i <= n; i++) {
		cin >> p[i] >> v[i]; 
		dep[i] = dep[p[i]] + v[i];
		sum += v[i];
		G[p[i]].pb(i);
	}
	ll L = 0,R = sum,mid;
	while(L < R) {
		mid = (L + R) >> 1;
		if(check(mid)) {
			R = mid;
		} else {
			L = mid+1;
		}
	}
	cout<<L<<'\n';
}
int main() {
	freopen("trip.in","r",stdin);
	freopen("trip.out","w",stdout);
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//	int T; cin >> T; while(T--)
	Solve();
	return 0;
}
